package com.web.service.impl;

import com.web.entity.DietRecord;
import com.web.mapper.DietRecordMapper;
import com.web.service.DietRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DietRecordServiceImpl implements DietRecordService {

    @Autowired
    private DietRecordMapper dietRecordMapper;

    @Override
    public List<DietRecord> getByUserIdAndDate(DietRecord dietRecord) {
        return dietRecordMapper.getByUserIdAndDate(dietRecord);
    }

    //增加饮食记录
    @Override
    public void add(DietRecord dietRecord) {
        //设置创建时间和更新时间
        dietRecord.setCreateTime(LocalDateTime.now());
        dietRecord.setUpdateTime(LocalDateTime.now());
        dietRecordMapper.insert(dietRecord);
    }

    //根据id删除饮食记录
    @Override
    public void deleteById(int id) {
        dietRecordMapper.deleteById(id);
    }

    //根据id修改饮食记录
    @Override
    public void update(int id,DietRecord dietRecord) {
        //设置更新时间
        dietRecord.setUpdateTime(LocalDateTime.now());
        //设置id
        dietRecord.setId(id);
        dietRecordMapper.updateById(dietRecord);
    }

    //根据id查询饮食记录
    @Override
    public DietRecord findById(int id) {
        return dietRecordMapper.selectById(id);
    }

    //查询所有饮食记录
    @Override
    public List<DietRecord> findAll() {
        return dietRecordMapper.selectAll();
    }

}
